<?php

include_once ('bn_path.php');

class bn_gadget
{
    private $dl;
    private $up;
    private $fd;
    private $dir;
    private $gf;
    protected static $instance;

    public function __construct()
    {
        $this->dl = new bn_dal();
        $this->up = new bn_upload();
        $this->fd = new bn_files();
        $this->dir = new bn_directory;
        $this->gf = new bn_genralfunc();
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function insert($gadname, $gadfile, $dir, $position, $status)
    {
        $this->dl->insert('gadgets', func_get_args());
    }

    public function update_status($status, $id)
    {
        return $this->dl->update('gadgets', array('status' => $status), array('id=' => $id));
    }

    public function update_position($position, $id)
    {
        return $this->dl->update('gadgets', array('position' => $position), array('id=' =>
                $id));
    }

    public function id2dir($id)
    {
        return $this->dl->getvalue('select dir from gadgets where id=?', $id);
    }

    public function unistall($dir)
    {
        $this->dl->delete('gadgets', $dir, 'dir');
    }

    public function delete($dir)
    {
        $this->dir->Delete_dir(bn_get_dir_gadget() . "/$dir");
        $this->dl->delete('gadgets', $dir, 'dir');
    }

    public function Get_gaddir($id)
    {
        $dir = $this->dl->getvalue('select filename from gadgets where id=?', $id);
        return basepath . '/gadgets/' . $row['filename'];
    }

    public function get_gadfile($dir)
    {
        $row = $this->dir->get_dirlist(basepath . "/gadgets/$dir/", 'file', 'php');
        return $row[0];
    }

    public function Select_allgadget($param = array())
    {
        return $this->dl->select("select * from gadgets", $param);
    }

    public function upload_gadget($file)
    {
        $dir = $this->gf->RandomStrgenerate();
        mkdir(basepath . '/gadgets/' . $dir, 0777, true);
        if ($this->up->upload($file, basepath . '/upload/module.zip', 'zip') == 1)
        {
            $this->fd->unzip_allfile(basepath . '/upload/module.zip', basepath . "/gadgets/$dir");
            unlink(basepath . '/upload/module.zip');
            return 1;
        } else
            return 2;
    }

    private function find_gadget($gadfile, $gadgets)
    {
        $cnt = count($gadgets);
        for ($i = 0; $i < $cnt; $i++)
        {
            if ($gadgets[$i]['dir'] == basename($gadfile))
                return $i;
        }
    }

    public function gadget_list()
    {
        $list = array();
        $gadgets = self::Select_allgadget()->fetch();
        $ret = array();

        foreach ($this->dir->get_dirlist(bn_get_dir_gadget(), 'dir') as $dir)
        {
            $i = self::find_gadget($dir, $gadgets);
            if (is_null($i))
            {
                $file = self::get_gadname($dir);
                //if($file!=null)
                $ret[] = array(
                    'name' => $file[0],
                    'filename' => $file[1],
                    'dir' => basename($dir),
                    'position' => '',
                    'status' => 'disable');
            } else
               $ret[] = $gadgets[$i];
        }
        return $ret;
    }

    public function get_gadname($dir)
    {
        global $hook;
        $hook = new bn_hookfunc;
        self::load_gadget(basename($dir), true);
        $gad_name = $hook->call_function('gadget_name');
        $gad_desc = $hook->call_function('gadget_describe');
        $gad_version = $hook->call_function('gadget_version');

        return array("$gad_name,$gad_version,$gad_desc", basename($file));
    }

    public function install_gadget($gaddir, $position = '')
    {
        $name = self::get_gadname($gaddir);
        if ($name != null)
        {
            return self::insert($name[0], $name[1], $gaddir, $position, 'enable');
        } else
            return 'file not found';
    }

    public function load_gadget($dir, $return = false)
    {
        if ($return)
        {
            ob_start();
            include_once (self::get_gadfile($dir));
            return ob_end_clean();
        } else
            include_once (self::get_gadfile($dir));
    }

    public function load_gadgets($position, $return = false)
    {
        $gads = $this->dl->select('select dir,position,filename from gadgets where status=show and position=?',
            $position);
        $out = '';
        foreach ($gads as $row)
        {
            if ($return)
            {
                ob_start();
                include_once (basepath . '/gadgets/' . $row['dir'] . '/' . $row['filename']);
                $out .= ob_get_clean();
            } else
                include_once (basepath . '/gadgets/' . $row['dir'] . '/' . $row['filename']);
        }
    }

    public function ddl_postion($selected='',$args=array('name' => 'gpos','cssclass' => 'select'))
    {
        echo $selected."<br />";
        $args['selected']=$selected;
        $arr = array("page","all","theme_left","theme_top","theme_bottom","theme_right","theme_center","theme_header","theme_footer","theme_nav");
        $ddl = new bn_DropDownList;
        $gf=new bn_genralfunc;
        $gf->class_property($ddl,$args);
        return $ddl->show($arr);
    }

    public function list_page_gadget($args=array('name' => 'plist_gadget','cssclass' => 'select'))
    {
        $lbx = new bn_listbox();
        $gf=new bn_genralfunc;
        $gf->class_property($lbx,$args);
        echo $lbx->show(self::Select_allgadget(array('position=' => 'page'))->fetch());
    }

    public function __destruct()
    {
        unset($this->dl);
        unset($this->up);
        unset($this->fd);
        unset($this->dir);
        unset($this->gf);
    }
}

?>